package top.lyf1210.yuwenpicturebackend.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import net.bytebuddy.implementation.bytecode.Throw;
import org.springframework.transaction.support.TransactionTemplate;
import org.springframework.web.bind.annotation.*;
import top.lyf1210.yuwenpicturebackend.common.BaseResponse;
import top.lyf1210.yuwenpicturebackend.common.DeleteRequest;
import top.lyf1210.yuwenpicturebackend.common.ResultUtils;
import top.lyf1210.yuwenpicturebackend.enums.PictureReviewStatusEnum;
import top.lyf1210.yuwenpicturebackend.enums.SpaceLevelEnum;
import top.lyf1210.yuwenpicturebackend.exception.BusinessException;
import top.lyf1210.yuwenpicturebackend.exception.ErrorCode;
import top.lyf1210.yuwenpicturebackend.exception.ThrowUtils;
import top.lyf1210.yuwenpicturebackend.model.Picture;
import top.lyf1210.yuwenpicturebackend.model.Space;
import top.lyf1210.yuwenpicturebackend.model.User;
import top.lyf1210.yuwenpicturebackend.model.dto.space.SpaceAddRequest;
import top.lyf1210.yuwenpicturebackend.model.dto.space.SpaceQueryRequest;
import top.lyf1210.yuwenpicturebackend.model.dto.space.SpaceUpdateRequest;
import top.lyf1210.yuwenpicturebackend.model.vo.space.SpaceLevel;
import top.lyf1210.yuwenpicturebackend.model.vo.space.SpaceVO;
import top.lyf1210.yuwenpicturebackend.service.SpaceService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.xml.transform.Result;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassNameSpaceController
 * @Description TODO
 * @Author钰玟
 * @Date2025/5/13 下午2:36
 * @Version 1.0
 **/
@RestController
@Slf4j
@RequestMapping("/space")
public class SpaceController {
    @Resource
    private SpaceService spaceService;


    @PostMapping("/update")
    @ApiOperation("修改空间")
    public BaseResponse<Boolean> updateSpace(@RequestBody SpaceUpdateRequest spaceUpdateRequest){
        ThrowUtils.throwIf(spaceUpdateRequest==null||spaceUpdateRequest.getId()<=0, ErrorCode.PARAMS_ERROR);
        Space space = new Space();
        //拷贝属性
        BeanUtil.copyProperties(spaceUpdateRequest, space);
        //自动填充
        spaceService.fillSpaceWithSpaceLevel(space);
        //校验
        spaceService.validSpace(space,false);
        //校验原来的数据
        Long id = space.getId();
        Space oldSpace = spaceService.getById(id);
        ThrowUtils.throwIf(oldSpace==null,ErrorCode.NOT_FOUND_ERROR);

        //更新
        boolean b = spaceService.updateById(space);
        ThrowUtils.throwIf(!b,ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(true);
    }
    @GetMapping("/list/level")
    @ApiOperation("获取等级列表")
    public BaseResponse<List<SpaceLevel>> listSpaceLevel() {
        List<SpaceLevel> list = Arrays.stream(SpaceLevelEnum.values()).map(
                        item->new SpaceLevel(
                                item.getValue(),
                                item.getText(),
                                item.getMaxCount(),
                                item.getMaxSize())
                )
                .collect(Collectors.toList());
        return ResultUtils.success(list);
    }


    @PostMapping("/delete")
    @ApiOperation("删除空间")
    public BaseResponse<Boolean> deleteSpace(@RequestBody DeleteRequest deleteRequest){
        ThrowUtils.throwIf(deleteRequest==null||deleteRequest.getId()<=0, ErrorCode.PARAMS_ERROR);
        spaceService.deleteSpace(deleteRequest.getId());
        return ResultUtils.success(true);
    }

    @PostMapping("/add")
    @ApiOperation("添加空间")
    /*
    * 管理员创建空间：管理员可以为指定用户创建空间。可以在创建空间时多传一个 userId 参数，但是要注意做好权限控制，仅管理员可以为别人创建空间。*/
    public BaseResponse<Long> addSpace(@RequestBody SpaceAddRequest spaceAddRequest){
//        ThrowUtils.throwIf(spaceAddRequest==null, ErrorCode.PARAMS_ERROR);
//        Long id = spaceService.addSpace(spaceAddRequest,null);
//        return ResultUtils.success(id);
        return null;
    }
    @GetMapping("/get")
    @ApiOperation("获取空间")
    public BaseResponse<SpaceVO> getSpaceVoById(@RequestParam Long id){
        ThrowUtils.throwIf(id<=0, ErrorCode.PARAMS_ERROR);
        Space space = spaceService.getById(id);
        SpaceVO spaceVO = new SpaceVO();
        spaceVO = SpaceVO.objToVo(space);
        ThrowUtils.throwIf(space==null,ErrorCode.NOT_FOUND_ERROR);
        return ResultUtils.success(spaceVO);
    }
    @PostMapping("/list/page/vo")
    @ApiOperation("获取空间列表")
    public BaseResponse<Page<SpaceVO>> listSpaceVoByPage(@RequestBody SpaceQueryRequest spaceQueryRequest,  HttpServletRequest request){
        //1. 获取分页参数
        long current = spaceQueryRequest.getCurrent();
        long pageSize = spaceQueryRequest.getPageSize();
        //限制爬虫
        ThrowUtils.throwIf(pageSize>20,ErrorCode.PARAMS_ERROR);
        //2.查询数据库
        Page<Space> page = spaceService.page(new Page<>(current,pageSize), spaceService.getQueryWrapper(spaceQueryRequest));

        return ResultUtils.success(spaceService.getPictureVOPage(page,request));
    }
    @PostMapping("/list/page")
    @ApiOperation("获取空间列表")
    public BaseResponse<Page<SpaceVO>> listSpaceByPage(@RequestBody SpaceQueryRequest spaceQueryRequest,  HttpServletRequest request){
        //1. 获取分页参数
        long current = spaceQueryRequest.getCurrent();
        long pageSize = spaceQueryRequest.getPageSize();
        //限制爬虫
        ThrowUtils.throwIf(pageSize>20,ErrorCode.PARAMS_ERROR);
        //2.查询数据库
        Page<Space> page = spaceService.page(new Page<>(current,pageSize), spaceService.getQueryWrapper(spaceQueryRequest));

        return ResultUtils.success(spaceService.getPictureVOPage(page,request));
    }
}
